﻿using Avalonia.Threading;
using HotAvalonia;

namespace AzrngTools;

internal sealed class Program
{
    // Initialization code. Don't use any Avalonia, third-party APIs or any
    // SynchronizationContext-reliant code before AppMain is called: things aren't initialized
    // yet and stuff might break.
    [STAThread]
    public static void Main(string[] args)
    {
        try
        {
            BuildAvaloniaApp()
                .StartWithClassicDesktopLifetime(args);

            Dispatcher.UIThread.UnhandledException += (_, e) =>
            {
                e.Handled = true;
                LocalLogHelper.WriteMyLogs("UI线程未处理异常", e.Exception.GetExceptionAndStack());
            };
            AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
            {
                if (e.ExceptionObject is Exception exception)
                    LocalLogHelper.WriteMyLogs("未处理异常", exception.GetExceptionAndStack());
            };
        }
        catch (Exception ex)
        {
            LocalLogHelper.WriteMyLogs("启动异常", ex.GetExceptionAndStack());
        }
    }

    private static AppBuilder BuildAvaloniaApp() =>
        AppBuilder.Configure<App>()
                  .UsePlatformDetect()
                  .UseHotReload() // 启用热重载核心引擎 [1,6](@ref)
                  .WithInterFont()
                  .LogToTrace();
}